/**********************************************
** @Des: login
** @Author: haodaquan
** @Date:   2017-09-07 16:30:10
** @Last Modified by:   haodaquan
** @Last Modified time: 2017-09-17 11:55:21
***********************************************/
package controllers

import (
	"strings"

	"newAi/new_web/libs"
	"newAi/new_web/models"
	"github.com/astaxie/beego"
)

type LoginController struct {
	BaseController
}

//登录 TODO:XSRF过滤
func (self *LoginController) LoginIn() {

	beego.ReadFromRequest(&self.Controller)
	username := strings.TrimSpace(self.GetString("username"))
	password := strings.TrimSpace(self.GetString("password"))
	if username != "" && password != "" {
		user, err := models.AdminGetByName(username)
		errorMsg := ""
		if user == nil {
			errorMsg = "帐号错误"
		} else {
			salt := strings.TrimSpace(user.Salt)
			if err != nil || user.Password != libs.Md5([]byte(password+salt)) {
				errorMsg = "帐号或密码错误" + libs.Md5([]byte(password+user.Salt))
			} else if user.Status == 0 {
				errorMsg = "该帐号已禁用"
			} else {
				self.ajaxMsg("成功", 1)
			}
		}
		self.ajaxMsg(errorMsg, 0)
	}
	self.ajaxMsg("失败", 0)
}

//登出
func (self *LoginController) LoginOut() {
	self.Ctx.SetCookie("auth", "")
	self.redirect(beego.URLFor("LoginController.LoginIn"))
}

func (self *LoginController) NoAuth() {
	self.Ctx.WriteString("没有权限")
}
